package org.vaadin.dotmap;

import org.vaadin.dotmap.client.ui.VDotMap;

import com.vaadin.annotations.Theme;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.terminal.WrappedRequest;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Root;
import com.vaadin.ui.VerticalLayout;

@Theme("dotmap")
public class DemoRoot extends Root {

    @Override
    protected void init(WrappedRequest request) {

        setContent(new Content());
    }

    static class Content extends VerticalLayout {

        Content() {
            setStyleName("black");
            setHeight("100%");

            final DotMap map = new DotMap();
            addComponent(map);

            map.setHeight("450px"); // crop from bottom

            // Controls follow

            HorizontalLayout hz = new HorizontalLayout();
            addComponent(hz);

            setExpandRatio(hz, 1);

            // Button that shows 5 random highlights
            Button b = new Button("5 random", new Button.ClickListener() {
                public void buttonClick(ClickEvent event) {
                    for (int i = 0; i < 5; i++) {
                        double lat = Math.random() * 180 - 90;
                        double lon = Math.random() * 360 - 180;
                        map.showHighlight(lat, lon);
                    }
                }
            });
            b.setDescription("Shows 5 random highlights using the currently selected hightlight image<br/>Shown under the dotmap and will obviously will not show if they end up in the water.");
            hz.addComponent(b);

            // allow dot color change, freely=allowNewItems
            ComboBox cb = new ComboBox();
            cb.setDescription("Dot color; set freely");
            cb.setNullSelectionAllowed(false);
            cb.setWidth("7em");
            cb.setImmediate(true);
            cb.setNewItemsAllowed(true);
            cb.addItem("#000");
            cb.addItem("#444");
            cb.addItem("#00b4f0");
            cb.addItem("#ee7c08");
            cb.addItem("#40b527");
            cb.addListener(new ValueChangeListener() {
                public void valueChange(ValueChangeEvent event) {
                    map.setDotColor((String) event.getProperty().getValue());
                }
            });
            cb.setValue(map.getDotColor());
            hz.addComponent(cb);

            // allow highlight image change (preconfigured)
            cb = new ComboBox();
            cb.setDescription("Highlight image (preconfigured)");
            cb.setNullSelectionAllowed(false);
            cb.setWidth("7em");
            cb.setImmediate(true);
            cb.setNewItemsAllowed(false);
            cb.addItem("red");
            cb.addItem("green");
            cb.addItem("blue");
            cb.addItem("orange");
            cb.addItem("black");
            cb.addItem("white");
            cb.addListener(new ValueChangeListener() {
                public void valueChange(ValueChangeEvent event) {
                    map.setHighlightImage(new ThemeResource(event.getProperty()
                            .getValue() + ".png"));
                }
            });
            cb.setValue("red");
            hz.addComponent(cb);

            // allow fade time change
            cb = new ComboBox();
            cb.setDescription("Fade time in msec");
            cb.setNullSelectionAllowed(false);
            cb.setWidth("7em");
            cb.setImmediate(true);
            cb.setNewItemsAllowed(false);
            cb.addItem(300);
            cb.addItem(3000);
            cb.addItem(10000);
            cb.addItem(99999);
            cb.addListener(new ValueChangeListener() {
                public void valueChange(ValueChangeEvent event) {
                    map.setFadeMsec((Integer) event.getProperty().getValue());
                }
            });
            cb.setValue(3000);
            hz.addComponent(cb);

            // Button that shows a custom highlight _over_ the map for 10
            // seconds
            b = new Button("Vaadin }>", new Button.ClickListener() {
                public void buttonClick(ClickEvent event) {
                    map.showHighlight(60.452647, 22.291903, new ThemeResource(
                            "vaadin.png"), 10000, VDotMap.ZINDEX_OVER);
                }
            });
            b.setDescription("Shows the Vaadin }&gt; logo on top of the map for 10 sec.");
            hz.addComponent(b);

        }
    }
}
